/*
 *  Copyright (C) 2008      Tri Tech Information Systems Inc.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef __PYXX_PY_QLIST_H__
#define __PYXX_PY_QLIST_H__

#include <QList>
#include <boost/python.hpp>

template< class Val >
struct PyQList
{
    typedef QList<Val> List;

    static const Val get( const List& self, int idx )
    {
        if( idx >= self.size() )
        {
            PyErr_SetString( PyExc_IndexError, "QList key not found" );
            boost::python::throw_error_already_set();
        }
        return self.value( idx );
    }

    static void set( List& self, int idx, const Val& val )
    {   self.replace( idx, val );
    }

    static void del( List& self, int idx )
    {   self.removeAt( idx );
    }

    static bool in( const List& self, const Val& idx )
    {   return self.contains( idx );
    }
};

template< class V >
void Wrap_QList()
{
    using namespace boost::python;

    class_< QList<V> >( typeid(QList<V>).name(), no_init )
        .def( "__len__", &QList<V>::size )
        .def( "__getitem__", &PyQList<V>::get )
        .def( "__setitem__", &PyQList<V>::set )
        .def( "__delitem__", &PyQList<V>::del )
        .def( "__contains__", &PyQList<V>::in )
        ;
}

#endif
